package com.khd.problems_0001_0100;

import java.util.ArrayList;
import java.util.List;

/**
 * 26. 删除有序数组中的重复项
 *
 * @author kehandi
 * @title: Problem_0025_RemoveDuplicatesFromSortedArray
 * @projectName algorithm
 * @date 2022/6/19 16:59
 */
public class Problem_0025_RemoveDuplicatesFromSortedArray {

    public static void main(String[] args) {
        int[] nums = new int[]{0, 0, 1, 1, 1, 2, 2, 3, 3, 4};
        int distinctLength = removeDuplicates(nums);
        System.out.println(distinctLength);


        List<Integer> list = new ArrayList<>();
        for (int num : nums) {
            list.add(num);
        }
        System.out.println(list);

    }

    public static int removeDuplicates(int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }
}
